package com.zktr.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zktr.pojo.Goods;
import com.zktr.pojo.OrderInfo;
import com.zktr.pojo.OrderInfoBean;
import com.zktr.pojo.PageBean;
import com.zktr.service.OrderinfoService;
import com.zktr.service.OrdersService;

/**
 * 订单表的Servlet
 */
@WebServlet("/OrdersServlet")
public class OrdersServlet extends BaseServlet{
	private static final long serialVersionUID = 1L;
    private OrdersService service=new OrdersService();
    private OrderinfoService serviceinfo=new OrderinfoService();
	/**
	 * 分页查询全部订单：
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void  findAllOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int pc = getPc(request);
		int ps = 3;
		PageBean<OrderInfoBean> pageBean = serviceinfo.findAllOrders(pc, ps);//传递pc, ps给Service，得到PageBean
		pageBean.setUrl(getUrl(request));
		request.getSession().setAttribute("pageBean",pageBean.getBeanList() );
		request.getSession().setAttribute("pb",pageBean);

		out.println("0");
	}
	/**
	 * 分页查询按条件得到结果：
	 */
	protected void  findAllOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		 OrderInfo info = encoding(request);
		int pc = getPc(request);
		int ps =3;	
		PageBean<OrderInfoBean> pageBean = serviceinfo.findOrders(info,pc, ps);//传递pc, ps给Service，得到PageBean
		pageBean.setUrl(getUrl(request));
		request.getSession().setAttribute("pageBean",pageBean.getBeanList() );
		request.getSession().setAttribute("pb",pageBean);
		out.println("0");
	}
	/**
	 * 订单详情
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void  findOneOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String orderid =request.getParameter("orderid");
		OrderInfo info=new OrderInfo();
		info.setOrderid(Integer.valueOf(orderid));
		//调用方法：
		OrderInfoBean x=serviceinfo.findOneOrder(info);
		request.getSession().setAttribute("order", x);
		if(x!=null) {
			out.println("0");
		}
		
	}
	/**
	 * 修改操作：
	 */
	protected void  updateOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到值并保存到info中：调用servlet方法：
		PrintWriter out = response.getWriter();
		String id=request.getParameter("id");
		String consignne=request.getParameter("consignne");
		double orderamount=Double.parseDouble(request.getParameter("orderamount"));
		double goodsamount=Double.parseDouble(request.getParameter("goodsamount"));
		String orderstatus=request.getParameter("orderstatus");
		OrderInfo info=new OrderInfo();
		info.setOrderamount(BigDecimal.valueOf(orderamount));
		info.setGoodsamount(BigDecimal.valueOf(goodsamount));
		orderstatus(orderstatus, info);
		info.setConsignne(consignne);
		int x=serviceinfo.updateorder(info, id);
		if(x>1) {
			out.println("0");
		}
	}
	/**
	 * 查询商品信息：
	 */
	
	protected void  findOneGoods(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到值并保存到info中：调用servlet方法：
		PrintWriter out = response.getWriter();
		String goodsname = request.getParameter("goodsname");
		Goods goods = service.findOneGoods(goodsname);
		if(goods!=null) {
			//封装数据：
			request.getSession().setAttribute("goods", goods);
		}else {
			out.println("0");
		}
		
	}
	/**
	 * 检查数量；
	 */
	
	protected void checknumber(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到值并保存到info中：调用servlet方法：
		PrintWriter out = response.getWriter();
		String goodsname = request.getParameter("goodsname");
		Goods goods = service.findOneGoods(goodsname);
		int number=Integer.valueOf(request.getParameter("goodsnumber"));
		if(goods.getGoodsnumber()<number||number<0) {
			out.println("0");
		}
	}
	/**
	 * 添加订单：
	 */
	
	protected void  addorder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//得到值并保存到info中：调用servlet方法：
		PrintWriter out = response.getWriter();
	}
	/**
	 * 发货单：
	 */
	protected void findsendOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			PrintWriter out = response.getWriter();
			int pc = getPc(request);
			int ps =3;	
			PageBean<OrderInfoBean> pageBean = serviceinfo.findsendOrders(pc, ps);//传递pc, ps给Service，得到PageBean
			pageBean.setUrl(getUrl(request));
			request.getSession().setAttribute("pageBean",pageBean.getBeanList() );
			request.getSession().setAttribute("pb",pageBean);
			out.println("0"); 
	}
	
	
	/**
	 * 处理get请求的中文编码
	 * @param request 
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	private OrderInfo encoding(HttpServletRequest request)  {
		String orderid=request.getParameter("orderid");
		String orderstatus=request.getParameter("orderstatus");
		String consignne=request.getParameter("consignne");
		OrderInfo info=new OrderInfo();
		if(orderid != null && !orderid.trim().isEmpty()) {
			info.setOrderid(Integer.valueOf(orderid));
		}
		
		if( orderstatus != null && ! orderstatus.trim().isEmpty()) {
			orderstatus(orderstatus, info);
			
		}
		
		if(consignne != null && !consignne.trim().isEmpty()) {
			info.setConsignne(consignne);
		}
		
		return info;
	}
	private void orderstatus(String orderstatus, OrderInfo info) {
		if (orderstatus.equals("确认")) {
			info.setOrderstatus(1);
		} else if(orderstatus.equals("未确认")) {
			info.setOrderstatus(0);
		} else if(orderstatus.equals("已取消")) {
			info.setOrderstatus(2);
		} else if(orderstatus.equals("无效")) {
			info.setOrderstatus(3);
		} else if(orderstatus.equals("退货")) {
			info.setOrderstatus(4);
		}
	}
	
	/**
	 *   得到当前页码：pc
	 *   如果pc参数不存在，默认pc=1
	 *   如果pc参数存在，需要转换成int类型即可
	 */
	private int getPc(HttpServletRequest request) {
		String value = request.getParameter("pc");
		if(value == null || value.trim().isEmpty()) {
			return 1;
		}
		try{
			return Integer.parseInt(value);
		}catch(Exception e){
			return  1;
		}
		
		
		
	}
	
	/**
	 * 截取url
	 *   /项目名/Servlet路径?参数字符串
	 * @param request
	 * @return
	 */
	private String getUrl(HttpServletRequest request) {
		String contextPath = request.getContextPath();//获取项目名
		String servletPath = request.getServletPath();//获取servletPath，即/CustomerServlet
		String queryString = request.getQueryString();//获取问号之后的参数部份
		
		//  判断参数部份中是否包含pc这个参数，如果包含，需要截取下去，不要这一部份。
		if(queryString.contains("&pc=")) {
			int index = queryString.lastIndexOf("&pc=");
			queryString = queryString.substring(0, index);
		}
		return contextPath + servletPath + "?" + queryString;
	}
}
